<template>
  <div>
    <div class="form">
      <el-form :inline="true" :model="pagination" class="demo-form-inline">
        <el-form-item label="配置名">
          <el-input
            v-model="pagination.name"
            placeholder="按 配置名 查找"
            clearable
            @clear="pagination.name = null"
          />
        </el-form-item>
        <el-form-item label="创建时间">
          <el-date-picker
            v-model="createTime"
            type="daterange"
            unlink-panels
            range-separator="到"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="YYYY-MM-DD"
            :shortcuts="shortcuts"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="reloadData">开始查找</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-table
      :data="tableData"
      style="width: 100%"
      :header-cell-style="{ 'text-align': 'center' }"
      :cell-style="{ 'text-align': 'center' }"
      stripe
      border
    >
      <el-table-column label="id" width="100" prop="id"> </el-table-column>
      <el-table-column label="配置名称">
        <template #default="scope">
          <el-tag>{{ scope.row.name }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="配置项值" prop="value"> </el-table-column>
      <el-table-column label="操作">
        <template #default="scope">
          <el-button size="small" @click="handleEdit(scope.row)"
            >编辑</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      class="pagination"
      v-model:current-page="pagination.page"
      v-model:page-size="pagination.size"
      :page-sizes="[10, 50, 100, 200]"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pagination.total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <el-dialog v-model="editing" title="编辑">
      <div class="config-name">
        <h1>配置名称: {{ editForm.name }}</h1>
      </div>
      <el-form :model="editForm" label-position="right">
        <el-form-item label="配置参数" label-width="140px">
          <el-input
            placeholder="请输配置参数"
            v-model="editForm.value"
          ></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span>
          <el-button @click="editing = false">取消</el-button>
          <el-button type="primary" @click="handleUpdateInfo"> 确定 </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { reactive } from "vue";
import { updateConfig, listConfig } from "@/api/adminAPI";
import { ElMessage } from "element-plus";

const pagination = reactive({
  page: 1,
  size: 10,
  total: 20,
});
const tableData = ref();
const editing = ref(false);
const editForm = ref({});
const shortcuts = [
  {
    text: "上周",
    value: () => {
      const end = new Date();
      const start = new Date();
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
      return [start, end];
    },
  },
  {
    text: "上月",
    value: () => {
      const end = new Date();
      const start = new Date();
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
      return [start, end];
    },
  },
  {
    text: "上三个月",
    value: () => {
      const end = new Date();
      const start = new Date();
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
      return [start, end];
    },
  },
];
const createTime = ref();

const reloadData = () => {
  listConfig(pagination, createTime.value).then(({ data }) => {
    const res = data.data.map((it) => {
      return {
        name: it.key,
        value: it.value,
      };
    });
    pagination.page = data.page;
    pagination.size = data.size;
    pagination.total = data.total;
    tableData.value = res;
  });
};

const handleSizeChange = (val) => {
  pagination.size = val;
  reloadData();
};
const handleCurrentChange = (val) => {
  pagination.page = val;
  reloadData();
};

const handleEdit = (row) => {
  editing.value = true;
  editForm.value = row;
};

const handleUpdateInfo = () => {
  editing.value = false;
  updateConfig(editForm.value).then((data) => {
    ElMessage.success(data.message);
    window.location.reload();
  });
};

reloadData();
</script>

<style scoped>
.pagination {
  margin-top: 20px;
}
.form {
  display: flex;
  margin-bottom: 10px;
}
.config-name {
  display: flex;
  justify-content: center;
}
</style>
